//
//  InternationalMediationController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/17.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "InternationalMediationController.h"
#import "MediationModel.h"
#import "InternationalMediationCell.h"
#import "InternationalMediationDetailController.h"


@interface InternationalMediationController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,assign) NSInteger currentPage;


@end

@implementation InternationalMediationController

static NSString *const ID = @"MediationCell";

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }

    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _string;
    self.tableView.backgroundColor = [UIColor whiteColor];

    __weak typeof (self) weakSelf = self;

    [self.tableView addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        
        [weakSelf MoreData];
        
    }];
}

- (void)loadData{
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': sstjList,'service':%@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:-1}'}",_service,userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        [self.dataArray removeAllObjects];
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict2 = dict[@"attrs"];
            
            MediationModel *model = [MediationModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray addObject:model];
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
       
        
    }];
    
    
}
- (void)MoreData{
    
    self.currentPage ++;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': sstjList,'service': %@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:-1}'}",_service,userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict2 = dict[@"attrs"];
            
            MediationModel *model = [MediationModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray addObject:model];
        }
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MediationModel *model = self.dataArray[indexPath.row];
    
    InternationalMediationCell *cell = [InternationalMediationCell WithInternationalMediationCell];
    
    if (cell == nil) {
        
        cell = [[InternationalMediationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MediationModel *model = self.dataArray[indexPath.row];

    InternationalMediationDetailController *inD = [[InternationalMediationDetailController alloc] init];

    inD.ID = model.ID;
    inD.service = _service;
    [self.navigationController pushViewController:inD animated:YES];

}



@end
